local chuanfang = fk.CreateSkill{
  name = "wk_heg__chuanfang",
}
Fk:loadTranslationTable{
  ["wk_heg__chuanfang"] = "传方",
  [":wk_heg__chuanfang"] = "当你的草花牌因弃置而置入弃牌堆后，你可以将其中一张交给一名其他角色或置于牌堆顶。",

  ["wk_heg__chuanfang_give"] = "交给一名其他角色",
  ["wk_heg__chuanfang_set"] = "置于牌堆顶",
  ["#wk_heg__chuanfang_give"] = "传方：将%arg交给一名其他角色"
}

local H = require "packages.ol_hegemony.util"
local HG = require "packages.new_heg_diy_my.util"

chuanfang:addEffect(fk.AfterCardsMove, {
  can_trigger = function (self, event, target, player, data)
    local all_cards = {}
    for _, move in ipairs(data) do
      if move.from == player and move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard then
        for _, info in ipairs(move.moveInfo) do
          if (info.fromArea == Player.Equip or info.fromArea == Player.Hand)and 
             player.room:getCardArea(info.cardId) == Card.DiscardPile then
            table.insert(all_cards, info.cardId)
          end
        end
      end
    end
    local cards = table.filter(all_cards, function(card)
      return Fk:getCardById(card).suit == Card.Club 
    end)
    cards = player.room.logic:moveCardsHoldingAreaCheck(cards)
    event:setCostData(self, {cards = cards, all_cards = all_cards})
    return #cards > 0
  end,
  on_cost = function (self, event, target, player, data)
    local cost_data = event:getCostData(self)
    local room = player.room
    local choices = {"wk_heg__chuanfang_set"}
    if #room:getOtherPlayers(player) > 0 then 
      table.insert(choices, "wk_heg__chuanfang_give")
    end
    local card, choice = room:askToChooseCardsAndChoice(player, {
      cards = cost_data.cards,
      all_cards = cost_data.all_cards,
      max_num = 1,
      min_num = 1,
      skill_name = chuanfang.name,
      choices = choices,
      cancel_choices = {"Cancel"},
      extra_data = {others = #room:getOtherPlayers(player) > 0},
    })
    if choice == "Cancel" then return end
    event:setCostData(self, {card = card, choice = choice})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    if cost_data.choice == "wk_heg__chuanfang_give" then
      local target = room:askToChoosePlayers(player, {
        targets = room:getOtherPlayers(player),
        min_num = 1,
        max_num = 1,
        skill_name = chuanfang.name,
        prompt = "#wk_heg__chuanfang_give:::"..Fk:getCardById(cost_data.card[1]):toLogString(),
        cancelable = false
      })
      if #target > 0 then
        room:obtainCard(target[1], cost_data.card, true, fk.ReasonGive, player, chuanfang.name)
      end
    else
      room:moveCards({
        ids = cost_data.card,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonPut,
        skillName = chuanfang.name,
        moveVisible = true,
        drawPilePosition = 1,
      })
    end
  end
})

return chuanfang